using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using SilverlightViewport;

namespace BoxTutorial
{
  /// <summary>
  /// This is the main type for your game
  /// </summary>
  public class Game1 : Microsoft.Xna.Framework.Game
  {
	GraphicsDeviceManager graphics;
	SpriteBatch spriteBatch;

	Matrix worldMatrix;
	Matrix cameraMatrix;
	Matrix projectionMatrix;
	float angle = 0f;

	BasicEffect cubeEffect;

	BasicShape cube = new BasicShape(new Vector3(6, 6, 6), new Vector3(0, 0, 0));

	XnaSilverlightComponent _silverlight;
	private Vector2 Size = new Vector2(611, 355);

	public Game1()
	{
	  graphics = new GraphicsDeviceManager(this);
	  Content.RootDirectory = "Content";

		_silverlight = new XnaSilverlightComponent(this);
		_silverlight.Size = Size;
		this.Components.Add(_silverlight);
	}

	/// <summary>
	/// Allows the game to perform any initialization
	/// it needs to before starting to run.
	/// This is where it can query for any required 
	/// services and load any non-graphic
	/// related content.  Calling base. Initialize will
	/// enumerate through any components
	/// and initialize them as well.
	/// </summary>
	protected override void Initialize()
	{
	  base.Initialize();
	  initializeWorld();
	}

	/// <summary>
	/// LoadContent will be called once 
	/// per game and is the place to load
	/// all of your content.
	/// </summary>
	protected override void LoadContent()
	{
	  // Create a new SpriteBatch, which can 
	  // be used to draw textures.
	  spriteBatch = new SpriteBatch(GraphicsDevice);
	}

	/// <summary>
	/// UnloadContent will be called once per game
	/// and is the place to unload all content.
	/// </summary>
	protected override void UnloadContent()
	{
	  // TODO: Unload any non ContentManager content here
	}

	/// <summary>
	/// Allows the game to run logic 
	/// such as updating the world,
	/// checking for collisions, 
	/// gathering input, and playing audio.
	/// </summary>
	/// <param name="gameTime">
	/// Provides a snapshot of timing values.</param>
	protected override void Update(GameTime gameTime)
	{
	  // Allows the game to exit
	  if(GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
		this.Exit();

	  angle += 0.5f;

	  base.Update(gameTime);
	}

	/// <summary>
	/// This is called when the game should draw itself.
	/// </summary>
	/// <param name="gameTime">
	/// Provides a snapshot of timing values.</param>
	protected override void Draw(GameTime gameTime)
	{
	  graphics.GraphicsDevice.Clear(Color.CornflowerBlue);

	  cubeEffect.Begin();

	  worldMatrix = Matrix.CreateRotationY(MathHelper.ToRadians(angle)) * Matrix.CreateRotationX(MathHelper.ToRadians(angle));
	cubeEffect.World = worldMatrix;

	  foreach(EffectPass pass in cubeEffect.CurrentTechnique.Passes)
	  {
		pass.Begin();

		cubeEffect.Texture = _silverlight.Texture;

		cube.RenderShape(GraphicsDevice);

		pass.End();
	  }
	  cubeEffect.End();

//	  base.Draw(gameTime);
	}

	public void initializeWorld()
	{
	  cameraMatrix = Matrix.CreateLookAt(new Vector3(0, 30, 20), new Vector3(0, 0, 0), new Vector3(0, 1, 0));
		projectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, Window.ClientBounds.Width / Window.ClientBounds.Height, 1.0f, 50.0f);
	  float tilt = MathHelper.ToRadians(22.5f);
	  worldMatrix = Matrix.CreateRotationX(tilt) * Matrix.CreateRotationY(tilt);

	  cubeEffect = new BasicEffect(GraphicsDevice, null);
	  cubeEffect.World = worldMatrix;
	  cubeEffect.View = cameraMatrix;
	  cubeEffect.Projection = projectionMatrix;
	  cubeEffect.TextureEnabled = true;
	}
  }
}
